//
//  UIImage+RQ.m
//  yidujob
//
//  Created by fc on 2017/4/21.
//  Copyright © 2017年 fengchao. All rights reserved.
//

#import "UIImage+RQ.h"

@implementation UIImage (RQ)

+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
}

+ (UIImage*)createImageWithColor:(UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

//手动实现图片压缩，可以写到分类里，封装成常用方法。按照大小进行比例压缩，改变了图片的size。
+ (UIImage *)makeThumbnailFromImage:(UIImage *)srcImage quality:(CGFloat)quality
{
    UIImage *thumbnail = nil;
    
    CGSize imageSize = CGSizeMake(360, 360);
    if (srcImage.size.width != imageSize.width || srcImage.size.height != imageSize.height) {
        UIGraphicsBeginImageContext(imageSize);
        CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);
        [srcImage drawInRect:imageRect];
        thumbnail = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    } else {
        thumbnail = srcImage;
    }
    
    NSData *imageData = UIImageJPEGRepresentation(thumbnail, quality);
    UIImage *newImage = [UIImage imageWithData:imageData];
   
    return newImage;
}

//字符串转图片
+ (UIImage *)Base64StrToUIImage:(NSString *)encodedImageStr
{
    NSData *decodedImageData = [[NSData alloc] initWithBase64EncodedString:encodedImageStr options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
    
    UIImage *decodedImage   = [UIImage imageWithData:decodedImageData];
    return decodedImage;
}
@end
